import { createRouter, createWebHashHistory } from 'vue-router'
import { TOKEN_KEY } from '@/global/constant'
import { localCache } from '@/utils/cache'

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: '/',
      redirect: '/main'
    },
    {
      path: '/login',
      component: () => import('../views/login/Login.vue')
    },
    {
      path: '/main',
      component: () => import('@/views/main/Main.vue'),
      children:[
        {
          path: '/main/user',
          component: () => import('@/views/main/system/user/index.vue'),
          // name: '角色管理',
        },
        {
          path: '/main/role', 
          component: () => import('@/views/main/system/role/index.vue'),
          // name: '角色管理',
        },
        {
          path: '/main/online',
          component: () => import('@/views/main/monitor/online/index.vue'),
          // name: '在线用户',
        } 
      ]
    },
    {
      path: '/:patchMatch(.*)',
      component: () => import('../views/not-found/NotFound.vue')
    }
  ]
})

router.beforeEach((to, form) => {
  const token = localCache.getCache(TOKEN_KEY)
  if (!token && to.path.startsWith('/main')) return '/login'
  // if (token) {
  //   const routers = localCache.getCache('userMenus')
  //   console.log({routers});
  //   router

  // }
})

export default router
